/**
 *  *2010-6-29 上午01:18:09  add 
 */
package com.base.myproject.client.HR.Salary;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.HR.bcode.HrBcodeSelectButton;
import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BaseObject;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncExeSql;
import com.base.myproject.client.tools.CommandSyncMakeCode;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.BaseTabPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.base.myproject.client.ui.dialog.ShowHtmlDialog;
import com.extjs.gxt.ui.client.GXT;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.NumberField;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Label;

/**
 * @author Administrator 2010-6-29 javazgw@gmail.com
 */
public class MakeSalaryX extends BaseJDBCPanel {
	Button makesalary = new Button("产生薪资");

	Button copybutton = new Button("复制信息");
	Button c = new Button("test");

//	TabPanel tp = new TabPanel();
	HrBcodeSelectButton hrbsb ;//= new HrBcodeSelectButton();
	ColumnConfig systembcodecolumn ;
	public MakeSalaryX() {
		init();
	}

	Dialog log = new Dialog();
	public void init() {

		initSelectGrid();

		initWeigetx();
		
		Label tip = new Label("请确认工资、税金、社保、花红策略和固定津贴的设置正确，确认宿舍、水电费、城镇/农村的内容完整！这些内容完整和正确才能保证工资的发放准确。");
		tip.setWidth("300");
		tip.setHeight("150");
		log.setHeading("提示");
		log.setLayout(new BorderLayout());
		log.add(tip, new BorderLayoutData(LayoutRegion.CENTER));
		log.setButtons(Dialog.OKCANCEL);
		log.setSize("320", "190");
		log.getButtonById(Dialog.OK).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				log.setVisible(false);
				addsalaryGrid();
			}
		});
		
		log.getButtonById(Dialog.CANCEL).addSelectionListener(new SelectionListener<ButtonEvent>(){

			@Override
			public void componentSelected(ButtonEvent ce) {
				log.setVisible(false);
			}
			
		});

		registerMainStore(store);
		registerStore(store);
		registerStore(bcode_store);
	
		hrbsb = new HrBcodeSelectButton()
		{
		public void doposted(List<TreeNodeModelData> modeldatalist) {

			for(TreeNodeModelData tnmd : modeldatalist)
			{
				HashMap hm = new HashMap();
				hm.put("systembcode", tnmd.get("systembcode"));
				hm.put("sheetcode", "MakeSalaryX");
				this.getTarget_store().add(hm);
			}
		}
		};
		hrbsb.setCheckNodes(CheckNodes.BOTH);
		hrbsb.setNeesreselect(true);
		hrbsb.setTarget_store(bcode_store);

		//渲染
		initrenderer();
		
	}

	public void complete() {
		super.complete();
		// ttiratify.setText("提交确认");
		// getToolbarll().add(ttiratify);
		 getToolbarll().add(copybutton);
	}

	/****************************************/

	SelectGrid zy_Grid;
	SelectGrid jobgroup_Grid;
	SelectGrid jobproperty_Grid;
	SelectGrid jobtype_Grid;

	void initSelectGrid() {
		// 创建多个Grid应用的table页中
		//职级
		zy_Grid = new SelectGrid(
				getStore(),
				"hr_range_zy_c",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'zy' ) as a left join  (select code,isset,upcode from hr_range_zy_c where upcode = '{code}' ) as b on a.code = b.code",
				"code","MakeSalaryX");
		
		// 雇佣组别
		jobgroup_Grid = new SelectGrid(
				getStore(),
				"hr_range_jobgroup",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobgroup' ) as a left join  (select code,isset,upcode from hr_range_jobgroup where upcode = '{code}' ) as b on a.code = b.code",
				"code","MakeSalaryX");
		// 雇佣性质
		jobproperty_Grid = new SelectGrid(
				getStore(),
				"hr_range_jobproperty",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobproperty' ) as a left join  (select code,isset,upcode from hr_range_jobproperty where upcode = '{code}' ) as b on a.code = b.code",
				"code","MakeSalaryX");
		// 雇佣类别
		jobtype_Grid = new SelectGrid(
				getStore(),
				"hr_range_jobtype",
				"select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'hr_jobtype' ) as a left join  (select code,isset,upcode from hr_range_jobtype where upcode = '{code}' ) as b on a.code = b.code",
				"code","MakeSalaryX");
		
		zy_Grid.grid.setHeight(255);
		jobgroup_Grid.grid.setHeight(255);
		jobproperty_Grid.grid.setHeight(255);
		jobtype_Grid.grid.setHeight(255);
		
		zy_Grid.setBorders(false);
		jobgroup_Grid.setBorders(false);
		jobproperty_Grid.setBorders(false);
		jobtype_Grid.setBorders(false);

	}

	private void initWeigetx() {
		copybutton.addListener(Events.Select, getListener());
	
		TabPanel panel = new TabPanel();
		panel.setPlain(true);
		panel.setSize(727, 285);

		TabItem zz_tab = new TabItem("组织");
		zz_tab.add(createBcodeGrid());

	
		zz_tab.setAutoHeight(true);
		panel.add(zz_tab);

		TabItem zy_tab = new TabItem("职级");
		zy_tab.addStyleName("pad-text");
		zy_tab.add(zy_Grid);
		panel.add(zy_tab);

		TabItem ji_neng = new TabItem("雇佣组别");// hr_jobgroup
		ji_neng.addStyleName("pad-text");
		ji_neng.add(jobgroup_Grid);
		panel.add(ji_neng);

		TabItem z_j = new TabItem("雇佣性质"); // hr_jobproperty
		z_j.addStyleName("pad-text");
		z_j.add(jobproperty_Grid);
		panel.add(z_j);

		TabItem lb_tab = new TabItem("雇佣类别"); // hr_jobtype
		lb_tab.addStyleName("pad-text");
		lb_tab.add(jobtype_Grid);
		panel.add(lb_tab);

		HorizontalPanel h = new HorizontalPanel();
		h.setSpacing(2);
		h.add(createGrid());

		VerticalPanel vp = new VerticalPanel();

		vp.setSpacing(1);
		vp.add(createForm());
		vp.add(panel);

		h.add(vp);
		add(h);
	}

	/***********************************/

	// private void initWeigetx() {

	/**
	 * add(new SelectGrid( getStore(), "hr_salary_zy_c","select a.code,a.name,isnull(b.isset,0) as isset,upcode from ( select code,name  from dictionary where type = 'zy' ) as a left join  (select code,isset,upcode from hr_salary_zy_c where upcode = '{code}' ) as b on a.code = b.code"
	 * , "code"));
	 **/

	// }

	BaseStore<BusiObject> store = null;

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("code");
		type.getField("code").setType(String.class);
		type.addField("Company");
		type.getField("Company").setType(String.class);
		type.addField("Salary_code");
		type.getField("Salary_code").setType(String.class);
		type.addField("Salary_code_old");
		type.getField("Salary_code_old").setType(String.class);
		type.addField("Summary");
		type.getField("Summary").setType(String.class);
		type.addField("Start_Time");
		type.getField("Start_Time").setType(Date.class);
		type.addField("End_Time");
		type.getField("End_Time").setType(Date.class);
		type.addField("Payment_date");
		type.getField("Payment_date").setType(Date.class);
		type.addField("Deadline");
		type.getField("Deadline").setType(String.class);
		type.addField("Including_salaries");
		type.getField("Including_salaries").setType(Boolean.class);
		type.addField("Including_bonuses");
		type.getField("Including_bonuses").setType(Boolean.class);
		type.addField("Including_allowances");
		type.getField("Including_allowances").setType(Boolean.class);
		type.addField("Allowance_Start_Date");
		type.getField("Allowance_Start_Date").setType(Date.class);
		type.addField("Allowance_End_Date");
		type.getField("Allowance_End_Date").setType(Date.class);
		type.addField("Including_attending");
		type.getField("Including_attending").setType(Boolean.class);
		type.addField("Attending_start_date");
		type.getField("Attending_start_date").setType(Date.class);
		type.addField("Attending_end_date");
		type.getField("Attending_end_date").setType(Date.class);
		type.addField("Payment_code");
		type.getField("Payment_code").setType(String.class);
		type.addField("Auto_Generate");
		type.getField("Auto_Generate").setType(Boolean.class);
		type.addField("Binary_method");
		type.getField("Binary_method").setType(String.class);
		type.addField("Last_payroll");
		type.getField("Last_payroll").setType(Boolean.class);
		type.addField("Join_Date");
		type.getField("Join_Date").setType(Date.class);
		type.addField("Payroll_month");
		type.getField("Payroll_month").setType(String.class);
		type.addField("Pay_period");
		type.getField("Pay_period").setType(String.class);
		type.addField("S_Payment_code");
		type.getField("S_Payment_code").setType(String.class);
		type.addField("Tax_set");
		type.getField("Tax_set").setType(String.class);
		type.addField("B_date");
		type.getField("B_date").setType(Double.class);
		type.addField("bonus_salaryno");
		type.addField("bonus_salaryordernum");
		type.addField("bonus_salarycode");
		
		type.getField("bonus_salaryordernum").setType(Integer.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_salary_main_c");
		info.setKeyname(new String[] { "icode" });
		info.setForcenolimit("true");
		info.setSql("select * from hr_salary_main_c order by code desc");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		store = new BaseStore<BusiObject>(type, info) {
			public boolean trySave() {
				if (this.getValue("code") == null) {
					Info.display("提示",  "保存时编码不能为空，请输入编码再保存，谢谢");
				} else {
					super.trySave();
				}
				return false;
			}
		};
		return store;
	}

	BaseGrid grid = null;

	BaseGrid createGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("code", "编码", 120);
		columns.add(C2);

		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid(getStore(), cm, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setSize(200, 465);
		return grid;
	}

	BaseFormPanel form = null;
	CheckBox C_num11;
	CheckBox C_num13;
	CheckBox C_num12;
	DateField C_num7;
	DateField C_num8;
	DateField C_num14;
	DateField C_num15;
	BaseComboBoxX C_bonus_salaryno;
	BaseComboBoxX C_bonus_salarycode;
	NumberField C_bonus_salaryordernum;
	TextField<String> C_num4 = new TextField<String>();

	BaseFormPanel createForm() {
		BaseFormPanel form = new BaseFormPanel(getStore());
		TableLayout tl = new TableLayout(6);

		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);
		form.setLayout(tl);
		form.setHeaderVisible(false);

		form.add(new Label("发薪编制号码:"), td);
		C_num4.setAllowBlank(false);
		C_num4.addListener(Events.Change, getListener());
		C_num4.setName("code");
		form.add(C_num4, td);
		form.add(new Label("摘要:"), td);
		TextField<String> C_num6 = new TextField<String>();
		C_num6.setName("Summary");
		form.add(C_num6, td);
		
		form.add(new Label("月工作平均天数:"), td);
		TextField<String> C_num28 = new TextField<String>();
		C_num28.setName("B_date");
		C_num28.setValue("21.75");
		form.add(C_num28, td);
			
		form.add(new Label("包括薪金:"), td);
		C_num11 = new CheckBox();
		C_num11.setName("Including_salaries");
		form.add(C_num11, td);
		
		form.add(new Label("开始日期:"), td);
		C_num7 = new DateField();
		C_num7.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num7.addListener(Events.Change, getListener());
		C_num7.setName("Start_Time");
		C_num7.setWidth(126);
		form.add(C_num7, td);

		form.add(new Label("完结日期:"), td);
		C_num8 = new DateField();
		C_num8.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num8.addListener(Events.Change, getListener());
		C_num8.setName("End_Time");
		C_num8.setWidth(126);
		form.add(C_num8, td);

		form.add(new Label("包括津贴:"), td);
		C_num13 = new CheckBox();
		C_num13.setName("Including_allowances");
		form.add(C_num13, td);

		form.add(new Label("津贴开始日期:"), td);
		C_num14 = new DateField();
		C_num14.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num14.addListener(Events.Change, getListener());
		C_num14.setName("Allowance_Start_Date");
		C_num14.setWidth(126);
		form.add(C_num14, td);

		form.add(new Label("津贴完结日期:"), td);
		C_num15 = new DateField();
		C_num15.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		C_num15.addListener(Events.Change, getListener());
		C_num15.setName("Allowance_End_Date");
		C_num15.setWidth(126);
		form.add(C_num15, td);
		
		
		form.add(new Label("包括花红:"), td);
		C_num12 = new CheckBox();
		C_num12.setName("Including_bonuses");
		form.add(C_num12, td);
		
		form.add(new Label("花红编码:"), td);
		C_bonus_salaryno = new BaseComboBoxX("salaryno","hr_salary_bonus", " where 1=1 group by salaryno");
		C_bonus_salaryno.setName("bonus_salaryno");
		form.add(C_bonus_salaryno, td);
		
		
		form.add(new Label("花红次数:"), td);
		C_bonus_salaryordernum = new NumberField();
		C_bonus_salaryordernum.setName("bonus_salaryordernum");
		form.add(C_bonus_salaryordernum, td);
		
		form.add(new Label(""), td);
		form.add(new Label(""), td);
		
		form.add(new Label("花红对应发薪编码:"), td);
		C_bonus_salarycode = new BaseComboBoxX("code","hr_salary_main_c", " where exists(select 1 from hr_salary where upcode = code)");
		C_bonus_salarycode.setName("bonus_salarycode");
		form.add(C_bonus_salarycode, td);
		
		form.add(new Label(""), td);
		form.add(new Label(""), td);

		form.add(new Label("包括考勤:"), td);
		CheckBox C_num16 = new CheckBox();
		C_num16.setName("Including_attending");
		form.add(C_num16, td);

		form.add(new Label("考勤开始日期:"), td);
		DateField C_num17 = new DateField();
		C_num17.setName("Attending_start_date");
		C_num17.setWidth(126);
		form.add(C_num17, td);

		form.add(new Label("考勤完结日期:"), td);
		DateField C_num18 = new DateField();
		C_num18.setName("Attending_end_date");
		C_num18.setWidth(126);
		form.add(C_num18, td);

		form.add(makesalary, td);
		makesalary.addListener(Events.Select, getListener());

	

		form.inited(140);
		return form;
	};

	BaseStore<BusiObject> bcode_store = null;

	BaseStore<BusiObject> getBcodeStore() {
		if (bcode_store == null) {
			createBcodeStore();
		}
		return bcode_store;
	}

	BaseStore<BusiObject> createBcodeStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("icode");
		type.getField("icode").setType(Long.class);
		type.addField("systembcode");
		type.getField("systembcode").setType(String.class);
		type.addField("upcode");
		type.getField("upcode").setType(String.class);
		type.addField("sheetcode");
		type.getField("sheetcode").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_range_bcode");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_range_bcode where sheetcode='MakeSalaryX' and upcode ='{code}'");
		 info.setParent(store);// 关联主表
		 info.setLinkparentcolumns(new String[] { "code" }); //关联主表字段
		 info.setLinkparent_chile_columns(new String[] { "upcode" });//
		// 关联主表字段
		bcode_store = new BaseStore<BusiObject>(type, info) {
			public java.util.Vector<String> _trySave() {
				java.util.Vector<String> v = new java.util.Vector<String>();
				if(store.getValue("code")==null)
				{
					Info.display("错误",  "系统内部错误，未找到主表code");
					return null;
					
				}
				
				/*for (int i=0; i<store.getCount(); i++) {
					int status = ((BaseObject) store.getModels().get(i))
					.getBusistatus();
					
					if ((status & BaseObject.DELETE) == BaseObject.DELETE) {
						String code = ((BaseObject) store.getModels().get(i)).get("code");
						String delRangeSql = "delete hr_range_bcode where sheetcode='MakeSalaryX' and upcode = '"+ code +"'";
						String delSalarySql = "delete hr_salary where upcode = '" +code+"'";
						String delZySql = "delete hr_range_zy_c where sheetcode='MakeSalaryX' and upcode = '"+ code +"'";
						String delGroupSql = "delete hr_range_jobgroup where sheetcode='MakeSalaryX' and upcode = '"+ code +"'";
						String delPropertySql = "delete hr_range_jobproperty where sheetcode='MakeSalaryX' and upcode = '"+ code +"'";
						String delTypeSql = "delete hr_range_jobtype where sheetcode='MakeSalaryX' and upcode = '"+ code +"'";
						v.add(delRangeSql);
					}
				}*/
				v.addAll(super._trySave());
				return v;
			}
		};
		return bcode_store;
	}

	BaseGridPanel bcodegrid = null;

	BaseGridPanel createBcodeGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		 columns.add(cbs.getColumn());


		systembcodecolumn = new ColumnConfig("systembcode", "部门人员", 80);
		columns.add(systembcodecolumn);
		ColumnConfig sheetcode= new ColumnConfig("sheetcode", "分类", 80);

		ColumnModel cm = new ColumnModel(columns);
		bcodegrid = new BaseGridPanel(getBcodeStore(), cm, true, true)

		{
			protected void addnewrecord() {

				addrecord();
			}
		};
		cbs.setSelectionMode(SelectionMode.SINGLE);
		bcodegrid.getBg().setSelectionModel(cbs);
		bcodegrid.getBg().setHeight(230);
		bcodegrid.getBg().setBorders(false);
		return bcodegrid;
	}

	
	void addrecord() {

		hrbsb.showEditor();
		// showEditor
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;

				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if (be.getSource().equals(copybutton)) {
								getcodeadncopy();
								

							} else if (be.getSource().equals(makesalary)) {
								if (C_num11.getValue()) {
									if (C_num7.getRawValue().equals("") || C_num8.getRawValue().equals("")) {
										MessageBox.alert("提示", "请填写完整的发薪日期，谢谢", null);
										C_num8.focus();
										return;
									}
								}
								
								if (C_num13.getValue()) {
									if (C_num14.getRawValue().equals("") || C_num15.getRawValue().equals("")) {
										MessageBox.alert("提示", "请填写完整的津贴日期，谢谢", null);
										C_num15.focus();
										return;
									}
								}
								
								if (C_num12.getValue()) {
									if (C_bonus_salaryno.getRawValue().equals("") || C_bonus_salaryordernum.getRawValue().equals("")) {
										MessageBox.alert("提示", "请填写完整的花红发放数据，谢谢", null);
										C_bonus_salaryno.focus();
										return;
									}
								}
								
								log.show();
							} else if (be.getSource().equals(c)) {
								Info.display("", "添加成功");
								String sql = sql_organization(zy_Grid);

							}

						} else if (be.getType() == Events.Change) {
							if (be.getSource().equals(C_num7)
									|| be.getSource().equals(C_num8)) {
								if (C_num7.getValue() != null
										&& C_num8.getValue() != null) {
									if (C_num8.getValue().before(
											C_num7.getValue())) {
										MessageBox.alert("提示",
												"薪金开始日期不能小于结束日期，请重新选择结束日期",
												null);
										C_num8.setValue(null);
									}
								}
							} else if (be.getSource().equals(C_num4)) {
								String newCode = C_num4.getRawValue();
								for (BusiObject bo : getStore().getModels()) {
									if (newCode.equals(bo.get("code"))) {
										MessageBox.alert("提示", "你输入的编码已经存在，请重新输入", null);
										C_num4.setRawValue("");
										break;
									}
								}
							} else {
								if (C_num15.getValue() != null
										&& C_num14.getValue() != null) {
									if (C_num15.getValue().before(
											C_num14.getValue())) {
										MessageBox.alert("提示",
												"津贴开始日期不能小于结束日期，请重新选择结束日期",
												null);
										C_num15.setValue(null);
										return;
									}
								}
							}
							
						} else if (be.getType().equals(Events.RowDoubleClick)) {
							if (be.getSource().equals(Salarygrid)) {
								showSalaryDetail();
								
							}
						}

						else if (be.getType() == Events.KeyUp) {
							if (be instanceof ComponentEvent) {
								if (((ComponentEvent) be).getKeyCode() == 13) // 回车
								{
								}
							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}

	/**
	 * 获得新的code
	 */
	void getcodeadncopy() {
		CommandSyncContainer list = new CommandSyncContainer();
		CommandSyncMakeCode csmc = new CommandSyncMakeCode();
		
		DateTimeFormat dtf = DateTimeFormat.getFormat("yyyy-MM-dd");
		Date d = new Date();
		
		csmc.setBeginstr(dtf.format(d));
		csmc.setColumnname("code");
		csmc.setTablename("hr_salary_main_c");
		list.add(csmc);

		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list,
				new AsyncCallback<CommandSyncReturnObject>() {
					public void onFailure(Throwable caught) {
						Window.alert("网络连接不稳定，请稍后重试！");
					}

					public void onSuccess(CommandSyncReturnObject cyro) {

						if (cyro.isB()) {

							String newcode = cyro.getRetrunstr();
							copy(newcode);

						} else {
							Window.alert("分配内码时出错。");
						}
					}
				});

	}

	/**
	 * 执行拷贝
	 * 
	 * @param newcode
	 */

	void copy(String newcode) {
		String code = grid.getStore().getValue("code").toString();// 当前行的code
		CommandSyncsql csql = new CommandSyncsql();

		String main_sql = "insert into hr_salary_main_c ( code,Company,Salary_code,start_time,end_time"
			 	+ ",Including_salaries,Including_allowances,Allowance_Start_Date,Allowance_End_Date,"
			 	+ "Including_bonuses,bonus_salaryno,bonus_salaryordernum,Including_attending,Attending_start_date,Attending_end_date) select top 1 '"
				+ newcode
				+ "' as code,Company,Salary_code,dateadd(month,1,start_time) ,dateadd(dd, -datepart(d, dateadd(dd, 4, dateadd(month,1,end_time))), dateadd(dd, 4, dateadd(month,1,end_time))),Including_salaries,Including_allowances,"
				+ "dateadd(month,1,Allowance_Start_Date),dateadd(dd, -datepart(d, dateadd(dd, 4, dateadd(month,1,Allowance_End_Date))), dateadd(dd, 4, dateadd(month,1,Allowance_End_Date))),"
				+ "Including_bonuses,bonus_salaryno,bonus_salaryordernum,Including_attending,dateadd(month,1,Attending_start_date),dateadd(dd, -datepart(d, dateadd(dd, 4, dateadd(month,1,Attending_end_date))), dateadd(dd, 4, dateadd(month,1,Attending_end_date))) from hr_salary_main_c where code ='"
				+ code + "'";
		GWT.log(main_sql);

		csql.getV().add(main_sql);

		// , , ,,
		String sql_2 = "insert into hr_range_zy_c (code,name,isset,upcode) select code,name,isset,'"
				+ newcode
				+ "' as upcode from hr_range_zy_c where sheetcode='MakeSalaryX' and upcode ='"
				+ code + "'";
		csql.getV().add(sql_2);

		String sql_3 = "insert into hr_range_jobgroup (code,name,isset,upcode) select code,name,isset,'"
				+ newcode
				+ "' as upcode from hr_range_jobgroup where  sheetcode='MakeSalaryX' and upcode ='"
				+ code
				+ "'";
		csql.getV().add(sql_3);

		String sql_4 = "insert into hr_range_jobproperty (code,name,isset,upcode) select code,name,isset,'"
				+ newcode
				+ "' as upcode from hr_range_jobproperty where sheetcode='MakeSalaryX' and upcode ='"
				+ code + "'";
		csql.getV().add(sql_4);

		String sql_5 = "insert into hr_range_jobtype (code,name,isset,upcode) select code,name,isset,'"
				+ newcode
				+ "' as upcode from hr_range_jobtype where sheetcode='MakeSalaryX' and upcode ='"
				+ code
				+ "'";
		csql.getV().add(sql_5);
		
		String sql_7 = "insert into hr_range_bcode (systembcode,upcode, sheetcode) select systembcode,'"
			+ newcode
			+ "' as upcode,sheetcode from hr_range_bcode where sheetcode='MakeSalaryX' and upcode ='"
			+ code + "'";
	csql.getV().add(sql_7);

		// Debug.printf(delsql, 4); 在控制台显示调试信息
		CommandSyncContainer list = new CommandSyncContainer();
		list.add(csql);
		// CommandSyncsql list.getCommandList().get(0);
		com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list,
				new AsyncCallback<CommandSyncReturnObject>() {
					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("错误", "网络连接失败", null);

					}

					@Override
					public void onSuccess(CommandSyncReturnObject result) {
						if (result.isB()) {
							Info.display("", "添加成功");

							store.reload();
						} else {
							MessageBox.alert("错误", "错误，错误代码"
									+ result.getMessage(), null);

						}
					}
				});
	}

	/**
	 * 组织sql查询条件
	 */
	public String sql_organization(SelectGrid g) {
		int g_count = zy_Grid.getStore().getCount(); // 获得grid的行数
		// String t=zy_Grid.grid.getValue(index, "isset").toString();//获得指定行的值
		// Boolean b=(Boolean) zy_Grid.grid.getValue(2,"isset");//判断isset是否被选中
		// 返回T F
		String where = null;
		String t;
		BaseGrid c = (BaseGrid) zy_Grid.grid.getValue("isset");
		// c.getValue(1, "isset").;

		GWT.log("yyyc:" + c.getValue(1, "isset"));
		// Boolean d=(Boolean)zy_Grid.grid.getValue(2,"isset");
		// t=zy_Grid.grid.getValue(1, "code").toString();
		// GWT.log("yyyd:"+t);
		for (int i = 1; i < g_count + 1; i++) {
			// t=zy_Grid.grid.getValue(i, "code").toString();
			// GWT.log("yyyy:"+t);
			// String b=zy_Grid.grid.getValue(i,"isset").toString();

			// if(b==null){
			// GWT.log("yyy2:"+b);
			// where="zy="+t+" or "+where;
			// }
		}
		// GWT.log("yyyy:"+where);
		return null;
	}

	BaseStore<BusiObject> Salarystore = null;

	BaseStore<BusiObject> getSalaryStore() {
		if (Salarystore == null) {
			createSalaryStore();
		}
		return Salarystore;
	}

	BaseStore<BusiObject> createSalaryStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("bcode");
		type.getField("bcode").setType(String.class);
		type.addField("amount1");
		type.getField("amount1").setType(Double.class);
		type.addField("amount2");
		type.getField("amount2").setType(Double.class);
		type.addField("amount3");
		type.getField("amount3").setType(Double.class);
		type.addField("amount4");
		type.getField("amount4").setType(Double.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("null");
		info.setKeyname(new String[] { "icode" });
		info
				.setSql("select bcode,sum(isnull(amount1,0)) as amount1,sum(isnull(amount2,0)) as amount2,sum(isnull(amount3,0)) as amount3,sum(isnull(amount4,0)) as amount4  from (select bcode, case when salarytype='1' then sum(amount) end as amount1,  case when salarytype='2' then sum(amount) end as amount2, case when salarytype='3' then sum(amount) end as amount3, case when salarytype='4' then sum(amount) end as amount4  from salary_view group by bcode,salarytype )as a group by bcode");

		info.setNeedAllRow("false");
		info.setForcenolimit("true");
		// info.setParent(mainstore);// 关联主表
		// info.setLinkparentcolumns(new String[] { "icode" }); // 关联主表字段
		// info.setLinkparent_chile_columns(new String[] { "upicode" });//
		// 关联主表字段
		Salarystore = new BaseStore<BusiObject>(type, info);
		return Salarystore;
	}

	BaseGrid Salarygrid = null;

	BaseGrid createSalaryGrid() {
		// final TreePanel<ModelData> tree = new TreePanel<ModelData>(null);
		// tree.setDisplayProperty("name");
		// // tree.getStyle().setLeafIcon(Resources.ICONS.music());
		// tree.setCheckable(true);
		// tree.setAutoLoad(true);
		// tree.getCheckedSelection()
		//		
		//		
		// //不知道你要做什么
		// tree.addCheckListener(new CheckChangedListener<ModelData>() {
		// @Override
		// public void checkChanged(CheckChangedEvent<ModelData> event) {
		//		  
		// }
		// });

		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C1 = new ColumnConfig("bcode", "bcode", 80);
		columns.add(C1);
		ColumnConfig C2 = new ColumnConfig("amount1", "amount1", 80);
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("amount2", "amount2", 80);
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("amount3", "amount3", 80);
		columns.add(C4);
		ColumnConfig C5 = new ColumnConfig("amount4", "amount4", 80);
		columns.add(C5);

		ColumnModel cm = new ColumnModel(columns);
		Salarygrid = new BaseGrid(createSalaryStore(), cm, false, false);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		Salarygrid.setSelectionModel(cbs);
		Salarygrid.setTrackMouseOver(true);
		Salarygrid.addListener(Events.RowDoubleClick, getListener());
		return Salarygrid;
	}

	void showSalaryDetail() {
		ShowHtmlDialog.getInstance().show(
				"asdfas<a><p>" + Salarygrid.getValue("amount1") + "</p>dd</a>",
				null);
	}

	void initrenderer()
	{
//		ht.put(((ModelData)combobox.getStore().getModels().get(i)).get(key).toString(), ((ModelData)combobox.getStore().getModels().get(i)).get(value)
//				.toString());
		
		
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
greetingService.getDataSet(
		"select systembcode,bname from systembcode_view ",
		new AsyncCallback<DataSet>() {

			@Override
			public void onFailure(Throwable caught) {
				Info.display("", "失败");
			}

			@Override
			public void onSuccess(final DataSet result) {
				Info.display("", "成功");
				 GridCellRenderer gcr = new GridCellRenderer()
				{

					@Override
					public Object render(ModelData model, String property,
							ColumnData config, int rowIndex, int colIndex,
							ListStore store, Grid grid) {
						// TODO Auto-generated method stub
						for(int i=0;i<result.getRowCount();i++)
						{
							
							
							if(result.getValue(i, "systembcode").equals(model.get(property)))
							{
							return	result.getValue(i, "bname");
							}
						}
						return model.get(property);
					
					}
					
				};
				systembcodecolumn.setRenderer(gcr);
				bcodegrid.getBg().getView().refresh(false);
			}

		});
	
		//return null;
		
	}
	void addsalaryGrid() {
		makesalary.setEnabled(false);
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(
				"select count(*) as c from hr_salary where upcode='"
						+ store.getValue("code").toString() + "'",
				new AsyncCallback<DataSet>() {

					@Override
					public void onFailure(Throwable caught) {

					}
					@Override
					public void onSuccess(DataSet result) {

						if (result.getValue(0, "c").equals("0")) {
							makesalary();
						} else {
							MessageBox.confirm("提示	", "该发薪编码已经存在发薪记录，是否更新？", new Listener<MessageBoxEvent> (){

								@Override
								public void handleEvent(MessageBoxEvent be) {
						
								
									if(be.getButtonClicked().getText().equals(GXT.MESSAGES.messageBox_yes()))
											{
												String sql = "delete hr_salary where upcode='" + store.getValue("code").toString() + "'";
												deleteSalary(sql);
												makesalary();
											}
								}
								
							});
						}
						makesalary.setEnabled(true);
					}

				});

	}

	void openSalary() {
		String icode = store.getValue("code").toString();
		Salary s= new Salary(icode);
		s.complete();
		BaseTabPanel.getInstance().addTab("薪资" + icode, s);
	}

	/**
	 * @param lists
	 *            要执行的sql语句
	 * @return 执行成功与否 执行所有的delete语句，完全申请表的提交操作
	 */
	private boolean deleteSalary(String sql) {
		CommandSyncsql csql = new CommandSyncsql();// 执行sql命令
		csql.getV().add(sql);
		if (csql.getV().size() <= 0) {
			return false;
		} else {
			CommandSyncContainer list = new CommandSyncContainer();// 可以添加的命令
			list.add(csql);

			GreetingServiceAsync greetingService = GWT
					.create(GreetingService.class);
			greetingService.SendCommandSync(list,
					new AsyncCallback<CommandSyncReturnObject>() {
						@Override
						public void onFailure(Throwable caught) {
							MessageBox.alert("错误", "网络连接失败", null);
						}

						@Override
						public void onSuccess(CommandSyncReturnObject result) {
							if (result.isB()) {
								
							} else {
								MessageBox.alert("错误", "错误，错误代码"
										+ result.getMessage(), null);

							}
						}
					});
		}
		return true;
	}
	
	void makesalary() {
		//先储存
		getStore().trySave();
		CommandSyncContainer list = new CommandSyncContainer();

		CommandSyncExeSql csgmd = new CommandSyncExeSql();
		HashMap<String, String> hm = new HashMap<String, String>();
		hm.put("upcode", store.getValue("code").toString());
		csgmd.setHm(hm);
		csgmd.setSqlname("hr_makesalary");

		list.add(csgmd);
		
		final MessageBox box = MessageBox.wait("薪金发放",  
	            "正在对该发薪编码所选人员范围进行发薪操作请等待...", "薪金记录生成中...");  

		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.SendCommandSync(list,
				new AsyncCallback<CommandSyncReturnObject>() {

					@Override
					public void onFailure(Throwable caught) {
						MessageBox.alert("错误", "网络连接失败", null);

					}

					@Override
					public void onSuccess(CommandSyncReturnObject result) {
						/*if (result.isB()) {
							Info.display("", "成功");
							openSalary();
						} else {
							MessageBox.alert("错误", "" + result.getMessage(),
									null);
						}*/
						box.close();
						Info.display("", "成功");
						openSalary();
					}

				});
	}
}
